package com.ymq.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ymq.reggie.common.CoustomExeception;
import com.ymq.reggie.entity.Category;
import com.ymq.reggie.entity.Dish;
import com.ymq.reggie.entity.Setmeal;
import com.ymq.reggie.mapper.CategoryMapping;
import com.ymq.reggie.service.CategoryService;
import com.ymq.reggie.service.DishService;
import com.ymq.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class CatergoryServiceImpl extends ServiceImpl<CategoryMapping, Category>implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void remove(Long id) {
        //查询当前id是否关联菜品
        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Dish::getCategoryId,id);
        int count = dishService.count(queryWrapper1);
        log.info("条数信息：{}",count);

        //异常处理器
        if(count>0){
           throw new CoustomExeception("当前分类关联了菜品");
        }
        //查询当前id是否关联套餐
        LambdaQueryWrapper<Setmeal> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(queryWrapper);
        if (count1>0){
            throw new CoustomExeception("当前分类关联了套餐");

        }
        //删除
        super.removeById(id);
    }
}
